import {Injectable} from '@angular/core';
import {Headers, Http} from '@angular/http';
import {HttpClient} from '@angular/common/http';
import {FarmPlantpower} from '../entity/farm-plant-power.model';
import {AppConfig} from '../config';
import {InterceptorService} from './interceptor/interceptor-service';

@Injectable()
export class FarmPlantPowerService {
  private readonly param = 'plantPowers';

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService,
              private http2: HttpClient) {
  }

  getFarmPlantPowers(): Promise<FarmPlantpoweTotal> {
    let url = `${this.url}/plantPower`;
    return this.http.get(url)
      .toPromise()
      .then(response => {
        return response.data as FarmPlantpoweTotal
      })
      .catch(this.handleError);
  }

  getFarmPlantPowerList(): Promise<FarmPlantpower[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => {
        return response.data as FarmPlantpower[]
      })
      .catch(this.handleError);
  }

  delete(id: number): Promise<any> {
    const url = `${this.url}/plantPowerId/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(res => {
        let result = res;
        switch (result.code) {
          case '200':
            return result;
          case '-1':
            throw new Error(result.message);
        }
      })
      .catch(this.handleError);
  }

  create(farmPlantpower: FarmPlantpower): Promise<FarmPlantpower> {
    // const url = `http://192.168.15.150:8585/webapi/v1/plantPowers`;
    return this.http.post(this.url, JSON.stringify(farmPlantpower))
      .toPromise()
      .then(res => {
        try {
          let result = res;
          switch (result.code) {
            case '200':
              return result.data;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }

  update(farmPlantpower: FarmPlantpower): Promise<FarmPlantpower> {
    const url = `${this.url}`;
    return this.http.put(url, JSON.stringify(farmPlantpower))
      .toPromise()
      .then(() => farmPlantpower)
      .catch(this.handleError);
  }


  hasFarmPlantpower(farmId: number, plantType: string, datePlant: string): Promise<boolean> {
    // const url = `http://192.168.15.150:8585/webapi/v1/plantPowers/getBasicPlantPowerByTypes`;
    const url = `${this.url}/getBasicPlantPowerByTypes`;
    return this.http.post(url, JSON.stringify({'farmId': farmId, 'plantType': plantType, 'datePlant': datePlant}))
      .toPromise()
      .then(res => {
        try {
          let result = res;
          switch (result.code) {
            case '3001':
              return true;
            case '3002':
              return false;
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);

  }

  importPlannedPower(file) {
    let formData = new FormData();
    formData.append('file', file[0], file[0].name);
    const url = `${this.url}/importPlannedPower`;
    return this.http.post(url, formData).toPromise().then(res => res).catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }

}

interface FarmPlantpoweTotal {
  yearList: FarmPlantpower[],
  dayList: FarmPlantpower[],
  monthList: FarmPlantpower[]
}
